package controllers;

import authentication.IMembershipProvider;
import authentication.annotations.Role;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.interceptor.multipart.UploadedFile;
import managers.WrongProductException;
import models.ProductImportList;
import models.ProductModel;
import services.ProductImportService;
import services.ProductsService;

import java.io.IOException;

import static br.com.caelum.vraptor.view.Results.json;

@Resource
public class AdminController extends BaseController {
    private final Result result;
    private final ProductsService productsService;
    private final ProductImportService importService;

    public AdminController(Result result,
                           ProductsService productsService,
                           IMembershipProvider membershipProvider,
                           ProductImportService importService) {
        super(result, membershipProvider);

        this.result = result;
        this.productsService = productsService;
        this.importService = importService;
    }

    @Role("admin")
    @Get
    public void addProduct(){
        result.include("product", new ProductModel());
    }

    @Role("admin")
    @Post
    public void addProduct(ProductModel product){

        try {
            productsService.saveProduct(product.generateEntity());
        } catch (WrongProductException e) {
            result.include("message", e.getMessage());
            result.include("product", product);
            return;
        }

        result.include("message", "Succeed");
        result.include("product", new ProductModel());
    }

    @Role("admin")
    public void closeAuction(int productId){
        productsService.closeAuction(productId);
        result.use(json()).from("Succeed").serialize();
    }

    @Get
    public void importProducts() {

    }

    @Post
    @Role("admin")
    public void importProducts(UploadedFile xml, byte format){
        if(xml == null){
            result.include("message", "File not provided");
            return;
        }

        try {
            ProductImportList list;
            if(format == ProductImportService.XML){
                list = importService.parseXml(xml.getFile());
            }else if(format == ProductImportService.SPRING){
                list = importService.readSpring(xml.getFile());
            } else {
                result.include("message", "Wrong format");
                return;
            }
            this.productsService.saveProductsFromImportedList(list);

        } catch (Exception e) {
            result.include("message", e.getMessage());
            return;
        } finally {
            try {
                xml.getFile().close();
            } catch (IOException e) {

            }
        }

        result.include("message", "Succeed");
    }
}
